﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;

public partial class Aspx_Redirect : System.Web.UI.Page
{

    KeRui.BLL.CmsComment bllComment = new KeRui.BLL.CmsComment();
    KeRui.Model.CmsComment modelComment = null;

    KeRui.BLL.CmsContent bllContent = new KeRui.BLL.CmsContent();
    KeRui.Model.CmsContent modelContent = null;

    KeRui.BLL.CmsNode bllNode = new KeRui.BLL.CmsNode();
    KeRui.Model.CmsNode modelNode = null;

    KeRui.BLL.CmsSite bllSite = new KeRui.BLL.CmsSite();
    KeRui.Model.CmsSite modelSite = null;

    /// <summary>
    /// 网站跳转 /aspx/redirect.aspx?commendid/contentid/nodeid/siteid={id}
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PageRequest.GetInt("commendid", -1) > 0)
        {
            RedirectCommend(PageRequest.GetInt("commendid", -1));
        }

        else if (PageRequest.GetInt("contentid", -1) > 0)
        {
            RedirectContent(PageRequest.GetInt("contentid", -1));
        }

        else if (PageRequest.GetInt("nodeid", -1) > 0)
        {
            RedirectNode(PageRequest.GetInt("nodeid", -1));
        }

        else if (PageRequest.GetInt("siteid", -1) > 0)
        {
            RedirectSite(PageRequest.GetInt("siteid", -1));
        }

        Response.End();
    }

    /// <summary>
    /// 网站跳转
    /// </summary>
    /// <param name="SiteID"></param>
    private void RedirectSite(int SiteID)
    {
        modelSite = bllSite.GetModelByCache(SiteID);
        if (modelSite == null)
        {
            Response.Write("该网站不存在。");
            Response.End();
            return;
        }

        Response.Redirect(modelSite.SiteUrl);
    }

    /// <summary>
    /// 栏目跳转
    /// </summary>
    /// <param name="NodeID"></param>
    private void RedirectNode(int NodeID)
    {
        modelNode = bllNode.GetModelByCache(NodeID);
        if (modelNode == null)
        {
            Response.Write("该栏目不存在。");
            Response.End();
            return;
        }

        modelSite = bllSite.GetModel(modelNode.SiteID);
        if (modelSite == null)
        {
            Response.Write("该栏目所属的网站不存在。");
            Response.End();
            return;
        }

        Response.Redirect(modelSite.SiteUrl + "/" + modelNode.NodePath + "/");
    }

    /// <summary>
    /// 内容跳转
    /// </summary>
    /// <param name="ContentID"></param>
    private void RedirectContent(int ContentID)
    {
        modelContent = bllContent.GetModel(ContentID);
        if (modelContent == null)
        {
            Response.Write("该内容不存在。");
            Response.End();
            return;
        }

        modelNode = bllNode.GetModelByCache(modelContent.NodeID);
        if (modelNode == null)
        {
            Response.Write("该内容所属的栏目不存在。");
            Response.End();
            return;
        }

        modelSite = bllSite.GetModelByCache(modelNode.SiteID);
        if (modelSite == null)
        {
            Response.Write("该内容所属的网站不存在。");
            Response.End();
            return;
        }

        Response.Redirect(modelSite.SiteUrl + "/"+modelNode.NodePath+"/" + modelContent.ContentID.ToString()+".shtml");

    }

    /// <summary>
    /// 评论跳转
    /// </summary>
    /// <param name="CommendID"></param>
    private void RedirectCommend(int CommendID)
    {
        modelComment = bllComment.GetModelByCache(CommendID);
        if (modelComment == null)
        {
            Response.Write("该评论不存在。");
            Response.End();
            return;
        }

        /*modelContent = bllContent.GetModelByCache(modelComment.ContentID);
        if (modelContent == null)
        {
            Response.Write("该评论所属的文章不存在。");
            Response.End();
            return;
        }

        modelNode = bllNode.GetModelByCache(modelNode.SiteID);
        if (modelNode == null)
        {
            Response.Write("该评论所属的栏目不存在。");
            Response.End();
            return;
        }*/

        modelSite = bllSite.GetModelByCache(modelComment.SiteID);
        if (modelSite == null)
        {
            Response.Write("该评论所属的网站不存在。");
            Response.End();
            return;
        }

        Response.Redirect(modelSite.SiteUrl + "/comment/" + modelComment.CommentID.ToString()+".shtml");

    }


}
